package com.jblog.controller.directive;

import static com.jblog.Constants.Encoding.UTF8;
import static com.jblog.Constants.Template.TPL_STYLE_LIST;
import static com.jblog.Constants.Template.TPL_SUFFIX;
import static com.jblog.common.web.freemarker.DirectiveUtils.OUT_BEAN;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.jblog.common.web.freemarker.DirectiveUtils;
import com.jblog.common.web.freemarker.exception.ParamsNotAtSameTimeException;
import com.jblog.common.web.freemarker.exception.ParamsRequiredException;
import com.jblog.core.model.blog.Post;
import com.jblog.core.model.blog.param.PostParam;
import com.jblog.core.service.facde.BlogFacade;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@SuppressWarnings({"unchecked", "rawtypes"})
public class PostDirective implements TemplateDirectiveModel {
    @Autowired protected BlogFacade iblog;
    
    //id first, then alias
    public static final String PARAM_ID     =  "id";
    public static final String PARAM_ALIAS  =  "alias";

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
            TemplateDirectiveBody body) throws TemplateException, IOException {
        
        Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
        paramWrap.put(OUT_BEAN, DEFAULT_WRAPPER.wrap(getData(params, env)));
        Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);

        env.include(TPL_STYLE_LIST + DirectiveUtils.getStyleList(params) + TPL_SUFFIX, UTF8, true);
        
        DirectiveUtils.removeParamsFromVariable(env, params, origMap);
    }
    
    private Long id(Map<String, TemplateModel> params) throws TemplateException {
        return DirectiveUtils.getLong(PARAM_ID, params);
    }
    private String alias(Map<String, TemplateModel> params) throws TemplateException {
        return DirectiveUtils.getString(PARAM_ALIAS, params);
    }
    
    private void checkParams(Map<String, TemplateModel> params) throws TemplateException {
        if (null == id(params) && null == alias(params)) {
            throw new ParamsRequiredException(PARAM_ID + " or " + PARAM_ALIAS);
        } 
        if (null != id(params) && null != alias(params)) {
            throw new ParamsNotAtSameTimeException(PARAM_ID + " or " + PARAM_ALIAS);
        }
    }
    
    public Post getData(Map<String, TemplateModel> params,
            Environment env) throws TemplateException {
        
        checkParams(params);
        
        PostParam postParam = new PostParam();
        postParam.setId(id(params));
        postParam.setAlias(alias(params));
        
        List<Post> posts = iblog.findPostByParam(postParam);
        return posts.size() > 0 ? posts.get(0) : null;
        
    }
}
